package 数组;

/**
 * @Author Weton Li
 * @Date 2021/5/1 10:25
 */
public class 斐波那契数 {
    public static void main(String[] args) {
        int input = 5;
        System.out.println(f(5));
        System.out.println(method2(5));
    }

    public static int f(int n){
        if (n<2) return n;
        return f(n-1)+f(n-2);
    }

    public static int method2(int n){

        if (n == 1) {
            return 1;
        }
        if (n == 0) {
            return 0;
        }

        // 0 1 1 2 3 5 8
        int a = 0;
        int b = 1;
        int c = 1;
        int i = 0;
        while (i<n-2){
            a = b;
            b = c;
            c = a+b;
            i++;
        }
        return c;
    }
}
